﻿using System.Data;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using StoreAPI.Models;
using StoreAPI.Services;
using StoreAPI.Services.ServerImpls;

namespace StoreAPI.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class StoreAreaController : ControllerBase
    {
        private readonly IStoreAreaService _StoreAreaService;
        public StoreAreaController(IStoreAreaService StoreAreaService)
        {
            _StoreAreaService = StoreAreaService;
        }
        //得到最大ID
        [HttpPost("GetMaxId")]
        public ActionResult GetMaxId()
        {
            var maxId = _StoreAreaService.GetMaxId();
            if (maxId != 0)
            {
                return Ok(new
                {
                    Code = 200,
                    Msg = "获取成功",
                    MaxId = maxId
                });
            }

            return Ok(new
            {
                Code = 10000,
                Msg = "获取失败"
            });
        }
        //是否存在该记录
        [HttpPost("IsExist")]
        public ActionResult Exists(int storeAreaId)
        {
            if (_StoreAreaService.Exists(storeAreaId))
            {
                return Ok(new
                {
                    Code = 200,
                    Msg = "存在"
                });
            }

            return Ok(new
            {
                Code = 10000,
                Msg = "不存在"
            });
        }

        //添加
        [HttpPost("AddStoreArea")]
        public ActionResult Add([FromBody] StoreArea model)
        {
            if (_StoreAreaService.Add(model))
            {
                return Ok(new
                {
                    Code = 200,
                    Msg = "添加成功"
                });
            }

            return Ok(new
            {
                Code = 10000,
                Msg = "添加失败"
            });
        }

        //更新
        [HttpPost("UpdateStoreArea")]
        public ActionResult Update([FromBody] StoreArea model)
        {
            if (_StoreAreaService.Update(model))
            {
                return Ok(new
                {
                    Code = 200,
                    Msg = "更新成功"
                });
            }

            return Ok(new
            {
                Code = 10000,
                Msg = "更新失败"
            });
        }
        //删除
        [HttpPost("DeleteStoreArea")]
        public ActionResult Delete([FromBody] int storeAreaId)
        {
            if (_StoreAreaService.Delete(storeAreaId))
            {
                return Ok(new
                {
                    Code = 200,
                    Msg = "删除成功"
                });
            }

            return Ok(new
            {
                Code = 10000,
                Msg = "删除失败"
            });
        }
        //批量删除数据
        [HttpPost("DeleteStoreAreas")]
        public ActionResult DeleteStoreAreas([FromBody] int[] storeAreaIds)
        {
            if (_StoreAreaService.DeleteList(storeAreaIds))
            {
                return Ok(new
                {
                    Code = 200,
                    Msg = "删除成功"
                });
            }

            return Ok(new
            {
                Code = 10000,
                Msg = "删除失败"
            });
        }
        //得到一个对象实体
        [HttpPost("GetStoreArea")]
        public ActionResult GetUser([FromBody] int storeAreaId)
        {
            var storeArea = _StoreAreaService.GetModel(storeAreaId);
            if (storeArea != null)
            {
                return Ok(new
                {
                    Code = 200,
                    Msg = "获取成功",
                    Data = storeArea
                });
            }

            return Ok(new
            {
                Code = 10000,
                Msg = "获取失败"
            });
        }
        //查询一行数据
        [HttpPost("DataRowToStoreArea")]
        public ActionResult DataRowToStoreArea([FromBody] DataRow dr)
        {
            var storeArea = _StoreAreaService.DataRowToModel(dr);
            if (storeArea != null)
            {
                return Ok(new
                {
                    Code = 200,
                    Msg = "转换成功",
                    Data = storeArea
                });
            }

            return Ok(new
            {
                Code = 10000,
                Msg = "转换失败"
            });
        }


        //获取全部用户
        [HttpPost("GetListAllStoreArea")]
        public ActionResult GetListAllStoreArea([FromBody] Expressionable<StoreArea> exp)
        {
            var storeArea = _StoreAreaService.GetListAll(exp);
            if (storeArea != null)
            {
                return Ok(new
                {
                    Code = 200,
                    Msg = "获取成功",
                    Data = storeArea
                });
            }

            return Ok(new
            {
                Code = 10000,
                Msg = "获取失败"
            });
        }
        //获得前几行数据
        [HttpPost("GetListStoreArea")]
        public ActionResult GetListUser([FromBody] Expressionable<StoreArea> exp, int Top)
        {
            var storeArea = _StoreAreaService.GetList(exp, Top);
            if (storeArea != null)
            {
                return Ok(new
                {
                    Code = 200,
                    Msg = "获取成功",
                    Data = storeArea
                });
            }

            return Ok(new
            {
                Code = 10000,
                Msg = "获取失败"
            });
        }
        //获取记录总数
        [HttpPost("GetRecordCountStoreArea")]
        public ActionResult GetRecordCountUser([FromBody] Expressionable<StoreArea> exp)
        {
            if (_StoreAreaService.GetRecordCount(exp) > 0)
            {
                return Ok(new
                {
                    Code = 200,
                    Msg = "获取成功",
                    Count = _StoreAreaService.GetRecordCount(exp)
                });
            }

            return Ok(new
            {
                Code = 10000,
                Msg = "获取失败"
            });

        }


        //分页
        [HttpPost("GetStoreAreaPage")]
        public ActionResult GetUserPage([FromBody] Expressionable<StoreArea> exp, int pageIndex = 1, int pageSize = 10)
        {
            var totalPage = 0;
            var storeArea = _StoreAreaService.GetListByPage(exp, out totalPage, pageIndex, pageSize);
            if (storeArea != null)
            {
                return Ok(new
                {
                    Code = 200,
                    Msg = "获取成功",
                    Data = storeArea,
                    TotalPage = totalPage
                });
            }

            return Ok(new
            {
                Code = 10000,
                Msg = "获取失败"
            });
        }
    }
}
